package com.usc.pys7.service;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usc.pys7.configs.OssConfig;
import com.usc.pys7.dao.StudentMapper;
import com.usc.pys7.module.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class StudentServiceImpl 
    extends ServiceImpl<StudentMapper ,Student>
        implements StudentService {
    @Autowired
    private OSS oss;
    @Autowired
    private OssConfig ossConfig;
    @Override
    public Student add(Student s) {
        MultipartFile photoFile=s.getPhotofile();
        String contentType=photoFile.getContentType();
        String originalFilename=photoFile.getOriginalFilename();
        String newfileName= UUID.randomUUID().toString().replace("-","")+""+originalFilename;
        String newfilepath="student/"+newfileName;
        try {
            PutObjectRequest putObjectRequest=new PutObjectRequest(ossConfig.getBucketName(),newfilepath,photoFile.getInputStream());
            oss.putObject(putObjectRequest);
            s.setPhoto(ossConfig.getUrlPrefix()+newfilepath);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("上传图片到oss失败");
        }
        s.setPhotofile(null);
        int r=this.baseMapper.insert(s);
        if(r>0){
            return baseMapper.selectById(s.getId());
        }
        throw new RuntimeException("添加学生失败");
    }

    @Override
    public void delete(int id) {
        int r=this.baseMapper.deleteById(id);
        if(r==0){  // 当没有记录被删除时（可能是ID不存在）
            throw new RuntimeException("删除学生失败");
        }
        // 删除成功则不需要返回任何值（方法返回类型为void）
    }

    @Override
    public Student get(int id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Student> findList() {
        return baseMapper.selectList(null);
    }
    @Override
    public List<Student> findByName(String name) {
        if (name != null && !"".equals(name)) {
            return baseMapper.selectList(new LambdaQueryWrapper<Student>().like(Student::getName, name));
        }
        return this.findList();
    }
    @Override

    public Student update(Student s) {
         if(s.getPhotofile()==null||s.getPhotofile().isEmpty()){
             Student oldstudent=  baseMapper.selectById(s.getId());
             s.setPhoto(oldstudent.getPhoto());
             int r=this.baseMapper.updateById(s);
             if(r>0){
                 return baseMapper.selectById(s.getId());
             }
             throw new RuntimeException("更新学生状态");
         }
        MultipartFile photoFile=s.getPhotofile();
        if(photoFile.isEmpty()){
           Student oldstudent=  baseMapper.selectById(s.getId());
            s.setPhoto(oldstudent.getPhoto());
           int r= baseMapper.updateById(s);
           if(r>0){
               return baseMapper.selectById(s.getId());
           }
           throw new RuntimeException("更新学生状态");
        }

        String originalFilename=photoFile.getOriginalFilename();
        String newfileName= UUID.randomUUID().toString().replace("-","")+""+originalFilename;
        String newfilepath="student/"+newfileName;
        try {
            PutObjectRequest putObjectRequest=new PutObjectRequest(ossConfig.getBucketName(),newfilepath,photoFile.getInputStream());
            oss.putObject(putObjectRequest);
            s.setPhoto(ossConfig.getUrlPrefix()+newfilepath);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("上传图片到oss失败");
        }
        s.setPhotofile(null);
        int r=this.baseMapper.updateById(s);
        if(r>0){
            return baseMapper.selectById(s.getId());
        }
        throw new RuntimeException("更新学生状态");
    }
}


